<?php 
class GenerateTrialLicenseForm extends Form
{
    const FIELD_NAME           = 'name';
    const FIELD_EMAIL          = 'email';
    const FIELD_PHONE          = 'phone';
	
    protected static $errorMap = array(
        'name_required'         => array('message' => 'Please enter your name', 'level' => self::ERROR_LEVEL_ERROR, 'field_name' => 'Name'),
        'email_required'        => array('message' => 'Please enter a valid e-mail address', 'level' => self::ERROR_LEVEL_ERROR, 'field_name' => 'Email'),
        'email_invalid'         => array('message' => 'Please enter existing e-mail address', 'level' => self::ERROR_LEVEL_ERROR, 'field_name' => 'Email'),
      );

    protected function GetTemplatePath()
    {
        return 'include/forms/generate_trial_license_form.tpl';
    }

    public function Setup()
    {
        $this->SetFields( array(
            self::FIELD_NAME, self::FIELD_EMAIL, self::FIELD_PHONE ) );
    }
    
    protected function Validate()
    {
        $name = preg_replace('/\s+/', ' ', $this->GetParamValue(self::FIELD_NAME, ''));
        if (!empty($name))
        {
            $this->SetValue(self::FIELD_NAME, $name);
        }
        else
        {
            $this->SetError(self::FIELD_NAME, self::$errorMap['name_required']);
        }
        $email = filter_var($this->GetParamValue(self::FIELD_EMAIL), FILTER_VALIDATE_EMAIL);
        if ($email !== false)
        {
            if (Util::checkMXRecord($email))
            {
                $this->SetValue(self::FIELD_EMAIL, $email);
            }
            else
            {
                $this->SetError(self::FIELD_EMAIL, self::$errorMap['email_invalid']);
            }
        }
        else
        {
            $this->SetError(self::FIELD_EMAIL, self::$errorMap['email_required']);
        }
        $phone = preg_replace('/\s+/', ' ', $this->GetParamValue(self::FIELD_PHONE, ''));
        $this->SetValue(self::FIELD_PHONE, $phone);
    }
}
?>